package com.breakfast.fun.ui.user.account;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.breakfast.fun.util.L;
import com.breakfast.fun.util.StatusBarUtil;
import com.breakfast.fun.view.widget.TitleBarView;
import com.breakfast.fun.R;

import butterknife.Bind;
import butterknife.ButterKnife;

public class UserAccountActivity extends AppCompatActivity {

    @Bind(R.id.tbv_user_account)
    TitleBarView tbvUserAccount;
    @Bind(R.id.wv_user_account)
    WebView wvUserAccount;

    private WebViewClient webViewClient = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return super.shouldOverrideUrlLoading(view, url);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            //页面加载完成了
            //设置页面的标题
            String userId = getIntent().getStringExtra("userId");
            String userSign = getIntent().getStringExtra("userSign");
            sendInfoToJs(userId, userSign);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_account);
        ButterKnife.bind(this);
        StatusBarUtil.transparent(getWindow());

        initView();
    }

    private void initView() {

        tbvUserAccount.setTitleNoRight("账户明细");

        tbvUserAccount.setBack(this);

        wvUserAccount.setWebViewClient(webViewClient);

        //允许运行脚本语言
        WebSettings settings = wvUserAccount.getSettings();
        settings.setJavaScriptEnabled(true);

        String url = "http://www.52letsgo.cn/fe/user/apphtml/account.html";
        wvUserAccount.loadUrl(url);
    }

    //在java中调用js代码
    public void sendInfoToJs(String userId, String userSign) {
        //调用js中的函数：showInfoFromJava(msg)
        wvUserAccount.loadUrl("javascript:showInfoFromApp('" + userId + "','" + userSign + "')");
        L.d(userId + "," + userSign);
    }
}
